

package top.continew.admin.question.user.service.questionset.entity;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import top.continew.admin.question.model.question.QuestionSet;
import top.continew.admin.question.model.question.QuestionSetQuestion;
import top.continew.admin.question.user.service.questionset.enums.QuestionSetTypeEnum;

/**
 * 专项练习题集实体类
 * 用于管理用户的专项练习题集
 */
@Slf4j
public class SpecialPracticeQuestionSet extends AbstractQuestionSet {

    private Long userId;

    //某个知识点下的专项练习(-1为通用专项练习)
    private Long knowledgeId;

    /**
     * 构造函数
     *
     * @param userId 用户ID
     */
    public SpecialPracticeQuestionSet(Long userId, Long knowledgeId) {
        this.userId = userId;
        this.knowledgeId = knowledgeId;
        // 查询用户是否已有专项练习题集，如果没有则创建
        QuestionSet existingQuestionSet = new QuestionSet().selectOne(Wrappers.lambdaQuery(QuestionSet.class)
            .eq(QuestionSet::getUserId, userId)
            .eq(QuestionSet::getType, QuestionSetTypeEnum.SPECIAL_PRACTICE.getCode())
            .eq(QuestionSet::getObjectId, knowledgeId));

        if (existingQuestionSet != null) {
            this.questionSet = existingQuestionSet;
        } else {
            // 如果不存在，创建新的专项练习题集
            this.createQuestionSet(new QuestionSet());
        }
    }

    /**
     * 构造函数 - 用于指定题集
     *
     * @param userId      用户ID
     * @param questionSet 指定的题集
     */
    public SpecialPracticeQuestionSet(Long userId, QuestionSet questionSet) {
        this.userId = userId;
        this.questionSet = questionSet;
    }

    /**
     * 创建专项练习题集
     *
     * @param questionSet 题集对象
     */
    @Override
    public void createQuestionSet(QuestionSet questionSet) {
        questionSet.setUserId(this.userId);
        questionSet.setTitle("专项练习");
        questionSet.setType(QuestionSetTypeEnum.SPECIAL_PRACTICE.getCode());
        questionSet.setObjectId(this.knowledgeId);
        // 调用父类方法创建题集
        super.createQuestionSet(questionSet);

        log.info("创建专项练习题集成功，用户ID: {}, 题集ID: {}", this.userId, questionSet.getId());
    }

    /**
     * 创建指定标题的专项练习题集
     *
     * @param title 题集标题
     * @return 创建的题集
     */
    public QuestionSet createSpecialPracticeWithTitle(String title) {
        QuestionSet questionSet = new QuestionSet();
        questionSet.setUserId(this.userId);
        questionSet.setTitle(title);
        questionSet.setType(QuestionSetTypeEnum.SPECIAL_PRACTICE.getCode());
        // 调用父类方法创建题集
        super.createQuestionSet(questionSet);

        log.info("创建专项练习题集成功，用户ID: {}, 题集ID: {}, 标题: {}", this.userId, questionSet.getId(), title);

        return questionSet;
    }

    /**
     * 获取用户ID
     *
     * @return 用户ID
     */
    public Long getUserId() {
        return userId;
    }

    @Override
    public void clearQuestion() {
        new QuestionSetQuestion().delete(Wrappers.lambdaQuery(QuestionSetQuestion.class)
            .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId()));
    }
}